package com.lefeng.cockpit.datax.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lefeng.cockpit.common.bean.dto.executor.ExecutorDTO;
import com.lefeng.cockpit.common.bean.po.datasource.DataSourcePO;
import com.lefeng.cockpit.common.bean.po.executor.ExecutorPO;
import com.lefeng.cockpit.common.bean.vo.execute.ExecutorStateVO;
import com.lefeng.cockpit.common.bean.vo.execute.ExecutorVO;
import com.lefeng.cockpit.datax.dao.ExecutorDao;
import com.lefeng.cockpit.datax.dao.mapper.ExecutorMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ExecutorDaoImpl implements ExecutorDao {

    @Resource
    private ExecutorMapper executorMapper;

    @Override
    public List<ExecutorPO> findByAddressType(int addressType) {

        LambdaQueryWrapper<ExecutorPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExecutorPO::getAddressType, addressType);
        return executorMapper.selectList(queryWrapper);
    }

    @Override
    public void update(ExecutorPO executorPO) {
        LambdaUpdateWrapper<ExecutorPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ExecutorPO::getId, executorPO.getId()); // WHERE 条件

        updateWrapper.set(ExecutorPO::getAddressList, executorPO.getAddressList()); // 更新值

        executorMapper.update(executorPO, updateWrapper);
    }

    @Override
    public IPage<ExecutorVO> selectPage(ExecutorDTO executorDTO) {

        // 创建分页对象
        Page<ExecutorVO> page = new Page<>(executorDTO.getPageNo(), executorDTO.getPageSize());
        // 执行查询
        return executorMapper.selectPageWithParams(page, executorDTO);
    }

    @Override
    public ExecutorStateVO getExecutorState() {
        // 执行查询
        return executorMapper.getExecutorState();
    }

    @Override
    public ExecutorPO load(int id) {
        return executorMapper.selectById(id);
    }

    @Override
    public DataSourcePO selectById(int datasourceId) {
        return null;
    }
}
